#include <iostream>
#include <string>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define EXIT 1

int main()
{
  pid_t id = fork();

  if (id < 0)
  {
    std::cout << "fork error " << errno << std::endl;
  }
  else if (id == 0)
  {
    // child
    int cnt = 5;
    while (cnt)
    {
      std::cout << "我是子进程: 我的pid是 :" <<getpid()<<"  我父亲的pid 是" <<getppid() << std::endl;
      sleep(1);
      cnt--;
    }
    exit(EXIT);
  }
  else
  {
    int status=0;
    gid_t rid = waitpid(id, &status, 0);
    if (rid > 0)
    {
      if (WIFEXITED(status))
      {
        printf("wait sub process success, rid: %d, status code: %d\n", rid, WEXITSTATUS(status)); // 
      }
      else
      {
        printf("child process quit error!\n");
      }
    }
    else
    {
      perror("waitpid");
    }
    while (true)
    {
      printf("我是父进程: pid:%d\n", getpid());
      sleep(1);
    }
  }

  return 0;
}